package com.awakensoftware.hunterserver.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.awakensoftware.hunterserver.repositories.TreasureRepository;
import com.awakensoftware.hunterserver.repositories.TreasureTemplateRepository;
import com.awakensoftware.hunterserver.serverentities.Treasure;
import com.awakensoftware.hunterserver.serverentities.TreasureTemplate;

@Service("treasureTemplateService")
@Scope("singleton")
@Transactional
public class TreasureTemplateService extends ServerEntityServiceBase<TreasureTemplate> {

	@Autowired
	private TreasureTemplateRepository treasureTemplateRepository;

	@Autowired
	private TreasureRepository treasureRepository;
	
	public List<TreasureTemplate> search(String text) {
		return treasureTemplateRepository.searchByName(text);
	}

	public TreasureTemplate create(String name) {
		TreasureTemplate treasureTemplate = new TreasureTemplate();
		treasureTemplate.setName(name);
		Treasure treasureInstance = new Treasure();
		treasureRepository.create(treasureInstance);
		treasureInstance.setTreasureTemplate(treasureTemplate);
		treasureTemplate.setTreasureInstance(treasureInstance);
		treasureTemplateRepository.create(treasureTemplate);
		return treasureTemplate;
	}

	public void update(TreasureTemplate treasureTemplate) {
		treasureRepository.update(treasureTemplate.getTreasureInstance());
		treasureTemplateRepository.update(treasureTemplate);
	}

}
